package com.example.tool.utils;

import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.Spacer;

import javax.swing.JComponent;
import javax.swing.JPanel;
import java.awt.Dimension;

/**
 * <p>Title: SwingUtil</p >
 * Description: SWing页面工具类
 * <p>CreateTime: 2022/6/29 15:02</p>
 * @author FT
 */
public class SwingUtil {

  private static String tips = null;

  public static void setTips(String tips) {
    SwingUtil.tips = tips;
  }

  @SuppressWarnings("All")
  public static void addChild(JPanel jPanel, JComponent jComponent, int row, int column) {
    addChild(jPanel, jComponent, row, column, false);
  }

  public static void addChild(JPanel jPanel, JComponent jComponent, int row, int column, boolean isFill) {
    if (column == 0 && !isFill) {
      jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
    } else if (isFill) {
      if (jComponent instanceof Spacer) {
        jPanel.add(jComponent, new GridConstraints(row, column, 1, 2, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_VERTICAL, GridConstraints.SIZEPOLICY_CAN_SHRINK, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
      } else {
        jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
      }
    } else {
      jPanel.add(jComponent, new GridConstraints(row, column, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_FIXED, null, new Dimension(300, -1), new Dimension(300, -1), 0, false));
    }
  }

  public static void addChild(JPanel jPanel, JComponent jComponent, int row) {
    jPanel.add(jComponent, new GridConstraints(row, 0, 1, 2, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
  }


  public static boolean IsItANumber(String text) {
    try {
      Integer.valueOf(text);
    } catch (Exception e) {
      return true;
    }
    return false;
  }

  @SuppressWarnings("all")
  public static boolean IsEnglish(String text) {
    return text.matches("^[a-zA-Z]*");
  }

  public static String promptText() {
    return "选择项目根目录即可。插件id必须为英文字符，插件编码必须为数字。";
  }

  public static String promptTextAction() {
    return "<html><body>选择项目根目录即可，插件id必须为英文字符。<br>创建文件时生成的xml将在指定目录下，如下: <br><span style=\"color: #F56C6C;\">/编译项目目录/WEB-INF/cfgHome/plugin/插件id/spring/</span></body></html>";
  }

  public static String promptTextActionAll() {
    return "请输入对应的名称，所有文件均不需要输入后缀。";
  }

  public static String promptTextActionTips() {
    return tips;
  }
}
